/**
 * Created by cz on 2017/4/11.
 */
$(function(){
    var Scope = {
        "getWeixinInfo":"./index.php?c=Index&a=getWeixinToken",
        "customerData":"./index.php?c=Index&a=customInfo"
    };
    $('#date6').datePicker({
        beginyear: 1950,
        theme: 'date',
    });
    var localtionURl = window.location.href.split("#")[0];
    console.log(localtionURl);
    $.ajax({
        url:Scope.getWeixinInfo,
        type:"GET",
        data:{url:localtionURl},
        dataType:"json",
        success: function (data) {
            weixinCallBack(data);
        }
    });
    function weixinCallBack(data){
/*        console.log(data.appId);
        console.log(data.string);
        console.log(data.url);
        console.log(data.signature);
        console.log(data.timestamp);
        console.log(data.nonceStr);
        console.log(data.jsapiTicket);*/
        wx.config({
            debug: false,
            appId:data.appId ,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: [
                'chooseImage',
                'uploadImage'
            ]
        });
    }
    $("#chooseImages").click(function () {
        //alert("fffaf");
        chooseImage();
    });
    function chooseImage(){
        // 选择张片
        wx.ready(function () {
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function(res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    $("#weixin_image").attr('src', localIds);
                    $("#imageUpload").css("display","none");
                    $("#user_test_answer").css("display","block");
                    // 上传照片
                    wx.uploadImage({
                        localId: '' + localIds,
                        isShowProgressTips: 1,
                        success: function(res) {
                            serverId = res.serverId;
                            $("#serverId").val(serverId); // 把上传成功后获取的值附上
                        }
                    });
                },error:function(res){
                    dialog.error("上传失败");
                }
            });
        });
    }
    function checkString(str,len,message){
        if(typeof str == "string"){
            if(!(str == "" || str == null || str == 'undefined')){
                if(str.length > len){
                    dialog.error(message);
                    return false;
                }
            }
        }else{
            dialog.error("不合法类型");
        }
    }
    $("#btn").click(function(){
        var phone = $('#mobile').val();
        var username = $("#user_name").val();
        var textArea = $("#addr").val();
        checkString(username,4,"姓名太长啦");
        checkString(textArea,30,"不能超过30字啦");
        if(!(/^1[34578]\d{9}$/.test(phone))){
            dialog.error("手机号码不合法");
            return false;
        }
        var userData = $("#customerInfo").serializeArray();
        //console.log(userData);
        var userInfo = {};
        if(Array.isArray(userData) && userData.length > 0){
            $(userData).each(function(i){
                userInfo[this.name] = this.value;
            });
            $.ajax({
                url:Scope.customerData,
                type:"POST",
                data:userInfo,
                dataType:"json",
                success: function (data) {
                    if(data.status == 0){
                        dialog.error(data.message);
                    }
                    else if(data.status == 1){
                        var url = "http://www.ftd1992.win/shengbei/shengbei_active/index.php?m=Home&c=Detail&a=index";
                        dialog.success(data.message,url);
                    }else{
                        dialog.error("未知错误");
                    }
                },
                error:function(data){
                    console.log(data);
                    dialog.alert("请求失败");
                }
            });
        }else{
            dialog.error("什么都没有填哦!");
        }
    })
});